package com.ewili.x.special.task.thread.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ewili.framework.utils.DateUtils;
import com.ewili.framework.utils.ExceptionUtils;
import com.ewili.framework.utils.ObjectHttpClientUtils;
import com.ewili.x.special.dao.inter.AmountStockDao;
import com.ewili.x.special.entity.AmountStock;
import com.ewili.x.special.entity.Stock;
import com.ewili.x.special.task.thread.inter.AmountStockThreadTask;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 * @author wonder
 */
@Slf4j
@Component
public class AmountStockThreadTaskImpl implements AmountStockThreadTask {

    @Autowired
    private AmountStockDao amountStockDao;

    //@Async("myTaskAsyncPool")
    @Override
    public void doSpide() {
        for (int i = 1; i < 1000000; i++) {

            try {
                String url = "http://push2.eastmoney.com/api/qt/clist/get?pn=" + i + "&pz=50&po=1&np=1&ut=b2884a393a59ad64002292a3e90d46a5&fltt=2&invt=2&fid0=f4001&fid=f69&fs=m:0+t:6+f:!2,m:0+t:13+f:!2,m:0+t:80+f:!2,m:1+t:2+f:!2,m:1+t:23+f:!2,m:0+t:7+f:!2,m:1+t:3+f:!2&stat=1&fields=f12,f14,f2,f3,f62,f184,f66,f69,f72,f75,f78,f81,f84,f87,f204,f205,f124&rt=53519345&cb=jQuery183048068368138030926_1605580353732&_=1605580367464";
                boolean shouldbreak = this.clearContent(url);
                if (shouldbreak) {
                    break;
                }
            } catch (Exception e) {
                ExceptionUtils.propagate(e);
            }
        }
    }

    private boolean clearContent(String url) {
        Connection connection = Jsoup.connect(url).timeout(3000000).ignoreContentType(true);
        connection.maxBodySize(0);

        String content = null;
        try {
            content = connection.get().body().text();
        } catch (IOException e) {
            ExceptionUtils.propagate(e);
        }

        String jsonContent = null;
        try {
            jsonContent = StringUtils.substringBetween(content, "(", ")");
        } catch (IndexOutOfBoundsException e) {
            System.out.println(e);
        }

        if (!jsonContent.contains("total")) {
            return true;
        }

        JSONObject object = JSONObject.parseObject(jsonContent);
        JSONObject dataObject = object.getJSONObject("data");
        JSONArray jsonArray = dataObject.getJSONArray("diff");


        Date date = DateUtils.currentDate(DateUtils.yyyy_MM_dd);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String code = jsonObject.getString("f12");
            Double close = jsonObject.getString("f2").equals("-") ? 0.0d : jsonObject.getDouble("f2");
            Double chg = jsonObject.getString("f3").equals("-") ? 0.0d : jsonObject.getDouble("f3");
            Double mainNetInflow = jsonObject.getString("f62").equals("-") ? 0.0d : jsonObject.getDouble("f62");
            Double mainNetInflowRate = jsonObject.getString("f184").equals("-") ? 0.0d : jsonObject.getDouble("f184");
            Double largeNetInflow = jsonObject.getString("f66").equals("-") ? 0.0d : jsonObject.getDouble("f66");
            Double largeNetInflowRate = jsonObject.getString("f69").equals("-") ? 0.0d : jsonObject.getDouble("f69");
            Double bigNetInflow = jsonObject.getString("f72").equals("-") ? 0.0d : jsonObject.getDouble("f72");
            Double bigNetInflowRate = jsonObject.getString("f75").equals("-") ? 0.0d : jsonObject.getDouble("f75");
            Double midNetInflow = jsonObject.getString("f78").equals("-") ? 0.0d : jsonObject.getDouble("f78");
            Double midNetInflowRate = jsonObject.getString("f81").equals("-") ? 0.0d : jsonObject.getDouble("f81");
            Double smallNetInflow = jsonObject.getString("f84").equals("-") ? 0.0d : jsonObject.getDouble("f84");
            Double smallNetInflowRate = jsonObject.getString("f87").equals("-") ? 0.0d : jsonObject.getDouble("f87");

            AmountStock amountStock = amountStockDao.findByCode(code);
            if (amountStock == null) {
                amountStock = new AmountStock();
            }
            amountStock.setCode(code);
            amountStock.setLastUpdateTime(date);
            amountStock.setClose(close);
            amountStock.setChg(chg);
            amountStock.setMainNetInflow(mainNetInflow);
            amountStock.setMainNetInflowRate(mainNetInflowRate);
            amountStock.setLargeNetInflow(largeNetInflow);
            amountStock.setLargeNetInflowRate(largeNetInflowRate);
            amountStock.setBigNetInflow(bigNetInflow);
            amountStock.setBigNetInflowRate(bigNetInflowRate);
            amountStock.setMidNetInflow(midNetInflow);
            amountStock.setMidNetInflowRate(midNetInflowRate);
            amountStock.setSmallNetInflow(smallNetInflow);
            amountStock.setSmallNetInflowRate(smallNetInflowRate);


            amountStockDao.save(amountStock);
        }

        return false;
    }

    @Override
    public void spideThreeDaysAmountAddRate(String url) {
        cn.hutool.json.JSONArray jsonArray = ObjectHttpClientUtils.formatEastMoneyJsonArray(url,"data");
        if (jsonArray == null) {
            return;
        }
        for (int i = 0; i < jsonArray.size(); i++) {
            cn.hutool.json.JSONObject jsonObject =  jsonArray.getJSONObject(i);
            Double threeDaysAmountAddRate = jsonObject.getDouble("f268");
            Double threeDaysLargeAmountAddRate = jsonObject.getDouble("f270");
            Double threeDaysBigAmountAddRate = jsonObject.getDouble("f272");
            Double threeDaysMidAmountAddRate = jsonObject.getDouble("f274");
            Double threeDaysSmallAmountAddRate = jsonObject.getDouble("f276");
            String code = jsonObject.getStr("f12");
            AmountStock amountStock =amountStockDao.findByCode(code);
            if(amountStock != null) {
                amountStock.setThreeDaysAmountAddRate(threeDaysAmountAddRate);
                amountStock.setThreeDaysLargeAmountAddRate(threeDaysLargeAmountAddRate);
                amountStock.setThreeDaysBigAmountAddRate(threeDaysBigAmountAddRate);
                amountStock.setThreeDaysMidAmountAddRate(threeDaysMidAmountAddRate);
                amountStock.setThreeDaysSmallAmountAddRate(threeDaysSmallAmountAddRate);
                amountStockDao.save(amountStock);
            }
        }

    }
}




